package im.yisme.view.ui;

import java.text.DecimalFormat;

import im.yisme.R;
import im.yisme.view.ui.animation.GameResultData;
import im.yisme.view.ui.animation.pojo.GameResultDataPOJO;
import im.yisme.view.ui.util.YViewThread;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.SurfaceHolder;

public class GameResultView extends YView{

	private Bitmap resultClassBmp, resultRateBmp, resultHitBmp, resultCountBmp, backBmp;
	private Bitmap starLightBmp, starDarkBmp, evaluateBmp, titleBmp, againBmp;
	private GameResultData dataThread;
	private GameResultDataPOJO data;
	private int evaluateScore;
	private boolean isStarVisible;
	
	public GameResultView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
		
	}
	
	private void init() {
		// TODO Auto-generated method stub
		initBitmap();
	}

	public void setScore(int score){
		evaluateScore = score;
	}
	public int getEvaluateScore(){
		return evaluateScore;
	}
	public void setStarVisible(boolean visibility){
		isStarVisible = visibility;
	}
	public void setData(int score, float accuracy, int maxCombo, String level){
		data = new GameResultDataPOJO();
		
		dataThread = new GameResultData(data, score, accuracy, maxCombo, level);
		dataThread.start();
	}
	
	

	private void initBitmap(){
		resultClassBmp = BitmapFactory.decodeResource(getResources(), R.drawable.result_class); 
		resultRateBmp = BitmapFactory.decodeResource(getResources(), R.drawable.result_rate); 
		resultHitBmp = BitmapFactory.decodeResource(getResources(), R.drawable.result_hit); 
		resultCountBmp = BitmapFactory.decodeResource(getResources(), R.drawable.result_count); 
		backBmp = BitmapFactory.decodeResource(getResources(), R.drawable.back); 
		starLightBmp = BitmapFactory.decodeResource(getResources(), R.drawable.star_1); 
		starDarkBmp = BitmapFactory.decodeResource(getResources(), R.drawable.star_0); 
		evaluateBmp = BitmapFactory.decodeResource(getResources(), R.drawable.evaluate); 
		titleBmp = BitmapFactory.decodeResource(getResources(), R.drawable.game_result_title); 
		againBmp = BitmapFactory.decodeResource(getResources(), R.drawable.game_result_again); 
	}

	@Override
	protected void createThread() {
		// TODO Auto-generated method stub
		paintThread = new GameResultViewThread(holder);
	}
	
	class GameResultViewThread extends YViewThread{

		public GameResultViewThread(SurfaceHolder holder) {
			super(holder);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected void paint(Canvas canvas) {
			// TODO Auto-generated method stub
			canvas.drawColor(Color.WHITE);
			
			Paint paint = new Paint();
			paint.setStrokeWidth(1);
			paint.setAntiAlias(true);
			
			canvas.drawBitmap(titleBmp, 640, 0, paint);
			canvas.drawBitmap(resultRateBmp, 525, 45, paint);
			canvas.drawBitmap(resultClassBmp, 580, 255, paint);
			canvas.drawBitmap(resultHitBmp, 15, 325, paint);
			canvas.drawBitmap(resultCountBmp, 170, 70, paint);
			canvas.drawBitmap(againBmp, 500, 435, paint);
			canvas.drawBitmap(backBmp, 750, 430, paint);
			
			
			if(isStarVisible){
			
				canvas.drawBitmap(evaluateBmp, 35, 30, paint);
				for (int i=0; i<5; i++){
					Bitmap starBmp;
					if (i >= evaluateScore)
						starBmp = starDarkBmp;
					else
						starBmp = starLightBmp;
					
					canvas.drawBitmap(starBmp, 35 + 75 * i, 120 - 25 * i, paint);
				}
				
			}
			
			
			
			// 画data
			if(data != null){
			   
				paint.setTextSize(30);
				
				canvas.drawText(data.getScore()+"", 325, 280, paint);
				canvas.drawText(data.getMaxCombo()+"", 60, 395, paint);
				DecimalFormat format = new DecimalFormat("00.00%");
				String text = format.format(data.getAccuracy());
				canvas.drawText(text, 560, 110, paint);
				
				paint.setTextSize(data.getLevelSize());
				paint.setAlpha(data.getLevelAlpha());
				
				Rect rect = new Rect(); 
				String text2 = data.getLevel();
				paint.getTextBounds(text2, 0, text2.length(), rect);
				canvas.drawText(text2, 650- rect.width()/2, 335 - rect.height()/2, paint);
			}
		}
		
	}
}
